package com.boredom.Guts;

import java.io.*;
import java.security.*;
import javax.crypto.*;
import com.boredom.Guts.Identity;

class Thing implements Serializable
{
    String name;

    public Thing(String name) {
	this.name = name;
    }
}


public class IdentityTest
{
    public static void test(String description, boolean passed) {
	if (passed)
	    System.out.printf("Test Passed: %s\n", description);
	else
	    System.out.printf("Test Failed! %s\n", description);
    }

    public static void main(String[] args) {
	Identity Alice = new Identity();
	Identity Bob = new Identity();

	Thing letter = new Thing("letter");
	Thing cat = new Thing("kitty!");

	byte[] signature = Alice.sign(letter);
	SealedObject catInBox = Alice.encrypt(cat);

	test("Bob accepts Alice's signature",
	     Bob.verify(letter, signature, Alice.getPublicKey()));
	test("Bob rejects Alice's signature on the wrong object",
	     !Bob.verify(cat, signature, Alice.getPublicKey()));
	test("Bob rejects his own signature in place of Alice's",
	     !Bob.verify(cat, Bob.sign(letter), Alice.getPublicKey()));
	test("Alice can retrieve her cat from her own box",
	     ((Thing) Alice.decrypt(catInBox)).name.equals("kitty!"));
	test("But Bob can't get at Alice's cat",
	     Bob.decrypt(catInBox) == null);
    }
}